package com.youxin.foundation.db.redis;

import java.util.Set;
import java.text.ParseException;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import com.youxin.foundation.utils.HostUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

/**
 * Redis Cluster Bean 从系统配置文件转换过来
 * @author yingde.cao
 *
 */
public class RedisClusterBean {
	protected int connectionTimeout=3000;
	protected int soTimeout=3000;
	protected int maxRedirections=3;
	private String hosts;
	protected GenericObjectPoolConfig poolConfig;
	private String name;
	protected JedisCluster jedisCluster;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public GenericObjectPoolConfig getPoolConfig() {
		return poolConfig;
	}

	public void setPoolConfig(GenericObjectPoolConfig poolConfig) {
		this.poolConfig = poolConfig;
	}

	public int getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public int getSoTimeout() {
		return soTimeout;
	}

	public void setSoTimeout(int soTimeout) {
		this.soTimeout = soTimeout;
	}

	public int getMaxRedirections() {
		return maxRedirections;
	}

	public void setMaxRedirections(int maxRedirections) {
		this.maxRedirections = maxRedirections;
	}

	public String getHosts() {
		return hosts;
	}

	public void setHosts(String hosts) {
		this.hosts = hosts;
	}

	public JedisCluster getRedis() throws ParseException {		
		if(jedisCluster!=null){
			return jedisCluster;
		}
		synchronized (this) {
			if(jedisCluster!=null){
				return jedisCluster;
			}			
			Set<HostAndPort> haps = HostUtils.parserHosts(hosts);					
			jedisCluster = new JedisCluster(haps, connectionTimeout, soTimeout, maxRedirections, poolConfig);
		}
		
		return jedisCluster;
	}
	
}
